package cn.edu.uestc.service;

import cn.edu.uestc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Service
public class SigninService {
    @Autowired
    HttpServletRequest request;
    @Autowired
    MongoTemplate mongoTemplate;

    public boolean signin(String username, String password) {
        Query query = new Query(Criteria.where("username").is(username));
        User user = mongoTemplate.findOne(query, User.class, "users");
        if (user != null && password.equals(user.getPassword())) {
            HttpSession session = request.getSession();
            session.setAttribute("username", user.getUsername());
            session.setAttribute("user_type", user.getType());
            return true;
        }
        return false;
    }
}
